package com.swq.java.code.chapter19.code13;

import java.awt.event.*;
import javax.swing.*;

public class OuterCounter extends JFrame {
    private JButton button = new JButton("1");

    public OuterCounter(String title) {
        super(title);
        //把MyListener的实例注册为JButton的监听器
        button.addActionListener(new MyListener(2));

        add(button);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(100, 100);
        setVisible(true);
    }

    public static void main(String args[]) {
        new OuterCounter("Hello");
    }
}

class MyListener implements ActionListener {
    private int step;  //决定JButton上的标号每次增加的步长

    public MyListener(int step) {
        this.step = step;
    }

    /**
     * 实现ActionListener的actionPerformed()方法
     */
    public void actionPerformed(ActionEvent evt) {
        JButton button = (JButton) evt.getSource();  //获得事件源
        int count = Integer.parseInt(button.getText());
        button.setText(new Integer(step + count).toString());  //把JButton上的标号加step
    }
}